/**
 * Created by HUANGFR2 on 8/24/2016.
 */
var app = angular.module('clientApp', ['ngRoute', 'user', 'order', 'merchant']);

app.config(function ($routeProvider) {
    $routeProvider
        .when('/', {
            redirectTo: 'merchant'
        })
        .when('/merchant', {
            templateUrl: '/merchant/views/merchant.client.view.html'
        })
        .when('/merchant/:id', {
            templateUrl: '/merchant/views/dish.client.view.html'
        })
        .when('/user', {
            templateUrl: '/user/views/user.client.view.html'
        })
        .when('/order/waitAndRecieve', {
            templateUrl: '/order/views/order.waitAndRecieve.view.html'
        })
        .when('/order/checkoutOrder/:mid', {
            templateUrl: '/order/views/order.checkout.view.html'
        })
        .when('/order/history', {
            templateUrl: '/order/views/order.history.view.html'
        })
});

app.run(function ($rootScope, $location, $http) {

    $rootScope.$on('$routeChangeStart', function () {
        var url = $location.url();
        if (url != '/merchant')
            $rootScope.showCom = false;
        else
            $rootScope.showCom = true;

        if(url=='/user'||url.indexOf("/order/")!=-1){
            var req = {
                method: "GET",
                url: "clients/getSession"
            };
            $http(req)
                .then(function (res) {
                    if (res.data.msg == "SUCCESS") {
                        $rootScope.user = res.data.client;
                    } else {
                        window.location.href='/login.html';
                    }
                });
        }
    });

});

//app.run(function ($rootScope, $location, $timeout, $http,merchantService) {
//    $rootScope.$on('$routeChangeStart', function () {
//        var url = $location.url();
//        $rootScope.permission = true;
//        if (url == '/myComplain' || url == '/ordersList' || url == '/newOrders' || url == '/dishList' || url == '/home' || url == '/updateInfo') {
//
//            merchantService.merchantInfo(function(data){
//                if (data) {
//                    $rootScope.merchant = data;
//                    $rootScope.permission = true;
//
//                } else {
//                    $rootScope.permission = false;
//                }